﻿#region Using Statements

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

#endregion

namespace StarWarsGame
{
    /// <summary>
    /// This is a game component that implements the Game Start Scene.
    /// </summary>
    public class PlayerSetupScene : GameScene
    {
        public Textbox txtBoxServerIP;

        public Textbox txtBoxNickName;

        public ImageButton playerSelectImage;

        // Spritebatch
        protected SpriteBatch spriteBatch = null;
        protected SpriteFont currentFont;
        string serverIP;
        KeyboardState oldKeyboardState;       

        public bool IsServer { get; set; }

        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="game">Main game object</param>
        /// <param name="smallFont">Font for the menu items</param>
        /// <param name="largeFont">Font for the menu selcted item</param>
        /// <param name="background">Texture for background image</param>
        /// <param name="elements">Texture with the foreground elements</param>
        public PlayerSetupScene(Game game, SpriteFont smallFont, SpriteFont largeFont,
                            Texture2D background, SpriteBatch currentSpriteBatch)
           : base(game)
        {
            Components.Add(new ImageComponent(game, background,
                                            ImageComponent.DrawMode.Center, currentSpriteBatch));

            //Game.Window.ClientBounds.Width / 2, 330
            playerSelectImage = new ImageButton(game, game.xwingImage, game.rebelImage, currentSpriteBatch, Game.Window.ClientBounds.Width / 2, 380);

            Components.Add(playerSelectImage);
            //string playerIsType = "client";
            //if (IsServer)
            //{
            //    playerIsType = "server";
            //}

            //Textbox setupPlayerTxt = new Textbox(game, smallFont, largeFont, currentSpriteBatch, 70, 50, 40, 400, game.borderTexture, "Setup player: ", "", );
            //setupPlayerTxt.Enabled = false;
            //Components.Add(setupPlayerTxt);
            //Textbox playerTxt = new Textbox(game, smallFont, largeFont, currentSpriteBatch, 70, 80, 40, 400, game.borderTexture, "Player is "+ playerIsType, "");
            //playerTxt.Enabled = false;
            //Components.Add(playerTxt);            



            //txtBoxServerIP
            serverIP = System.Configuration.ConfigurationSettings.AppSettings["ServerIP"];
            if (IsServer)
            {
                //TODO get current machine ip
                serverIP = "localhost";
            }
          
            txtBoxServerIP = new Textbox(game, smallFont, largeFont, currentSpriteBatch, 100, 150, 40, 400, game.borderTexture, "Server IP: ", serverIP, 15);
            txtBoxServerIP.Enabled = false;
            Components.Add(txtBoxServerIP);

            currentFont = smallFont;      

            // Get the current spritebatch
            spriteBatch = currentSpriteBatch;
      
            //txtBoxNickName
            txtBoxNickName = new Textbox(game, smallFont, largeFont, currentSpriteBatch, Game.Window.ClientBounds.Width / 2, 330, 40, 400, game.borderTexture, "Player Name: ", "", 18);            
            Components.Add(txtBoxNickName);
        }
      
        /// <summary>
        /// Show the start scene
        /// </summary>
        public override void Show()
        {         
            txtBoxServerIP.parsedText = serverIP;
            if (IsServer)
            {
                txtBoxServerIP.parsedText = System.Configuration.ConfigurationSettings.AppSettings["ServerIP"];
            }

            txtBoxNickName.parsedText = "";        
            base.Show();
        }

        /// <summary>
        /// Hide the start scene
        /// </summary>
        public override void Hide()
        {
            // backMusic.Stop(AudioStopOptions.Immediate);
            base.Hide();
        }        

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            KeyboardState keyboardState = Keyboard.GetState();

            bool result = ((oldKeyboardState.IsKeyDown(Keys.Tab) &&
                (keyboardState.IsKeyDown(Keys.Tab))) || (oldKeyboardState.IsKeyDown(Keys.Up) &&
                (keyboardState.IsKeyDown(Keys.Up)))
                || (oldKeyboardState.IsKeyDown(Keys.Down) &&
                (keyboardState.IsKeyDown(Keys.Down)))
                  || (oldKeyboardState.IsKeyDown(Keys.Left) &&
                (keyboardState.IsKeyDown(Keys.Left)))
                  || (oldKeyboardState.IsKeyDown(Keys.Right) &&
                (keyboardState.IsKeyDown(Keys.Right)))


                );

            if (IsServer)
            {
                txtBoxNickName.Enabled = true;
                txtBoxServerIP.Enabled = false;
            }
            else
            {
                if (result && txtBoxServerIP.Enabled)
                {
                    txtBoxNickName.Enabled = true;
                    txtBoxServerIP.Enabled = false;
                }
                else if (result && txtBoxNickName.Enabled)
                {
                    txtBoxNickName.Enabled = false;
                    txtBoxServerIP.Enabled = true;
                }
            }               
            oldKeyboardState = keyboardState;
            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            //spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

            //string playerIsType = "client";
            //if (IsServer)
            //{
            //    playerIsType = "server";
            //}
            //spriteBatch.DrawString(currentFont, "Setup player: ", new Vector2(10, 45), Color.Blue);
            //spriteBatch.DrawString(currentFont, "Player is " + playerIsType + " : ", new Vector2(10, 65), Color.Blue);
           
            //spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}